//
//  LoginPage.m
//  HB
//
//  Created by 孙智慧 on 16/1/26.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "LoginPage.h"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
#import "LoginAction.h"
#import "RegistSetupOne.h"
#import "DBHelper.h"

@implementation LoginPage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) viewWillAppear:(BOOL)animated
{    
    [super viewWillAppear:animated];
    
}

//视图显示事件 设置播放参数 并注册通知
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.avplayer seekToTime:kCMTimeZero];
    [self.avplayer setVolume:0.0f];
    [self.avplayer play];
    [self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(helpPlayerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[self.avplayer currentItem]];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //加载视频
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"welcome" ofType:@"mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
    AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
    self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
    AVPlayerLayer *avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
    //设置视频的显示状态
    [avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    
    //AVLayerVideoGravityResizeAspect
    //AVLayerVideoGravityResizeAspectFill
    //AVLayerVideoGravityResize
    
    //设置视图的显示区域
    [avPlayerLayer setFrame:[UIScreen mainScreen].bounds];
    //把播放视图加到主视图的layer
    [self.videoView.layer addSublayer:avPlayerLayer];
    
    UIButton *loginBtn=[[UIButton alloc] init];
    
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal] ;
    [loginBtn setBackgroundColor:[UIColor whiteColor]];
    loginBtn.translatesAutoresizingMaskIntoConstraints= NO;
    [loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.layer.cornerRadius = 2.0;
    [self.videoView addSubview: loginBtn];
    
    
    NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:loginBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeWidth multiplier:0.5f constant:-10.0f];
    
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:loginBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:45.0f];
    
    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:loginBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
    
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:loginBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-5.0f];
    
    [self.videoView addConstraints:@[widthConstraint,heightConstraint,leftConstraint,bottomConstraint ]];
    
    UIButton *RegistBtn=[[UIButton alloc] init];
    
    [RegistBtn setTitle:@"注册" forState:UIControlStateNormal];
    [RegistBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal] ;
    [RegistBtn setBackgroundColor:[UIColor redColor]];
    RegistBtn.translatesAutoresizingMaskIntoConstraints= NO;
    [RegistBtn addTarget:self action:@selector(registBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    RegistBtn.layer.cornerRadius = 2.0;
    [self.videoView addSubview: RegistBtn];
    
    
    NSLayoutConstraint *widthConstraint1 = [NSLayoutConstraint constraintWithItem:RegistBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeWidth multiplier:0.5f constant:-10.0f];
    
    NSLayoutConstraint *heightConstraint1 = [NSLayoutConstraint constraintWithItem:RegistBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:45.0f];
    
    NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:RegistBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
    
    NSLayoutConstraint *bottomConstraint1 = [NSLayoutConstraint constraintWithItem:RegistBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.videoView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-5.0f];
    
    [self.videoView addConstraints:@[widthConstraint1,heightConstraint1,rightConstraint,bottomConstraint1 ]];
    
}
-(void) loginBtnClick:(id)sender
{
    LoginAction *lAction=[[LoginAction alloc] init];
    [self.navigationController pushViewController:lAction animated:true];
}

-(void) registBtnClick:(id)sender
{
    RegistSetupOne *registSetupOne=[[RegistSetupOne alloc] init];
    [self.navigationController pushViewController:registSetupOne animated:true];
}

-(void)action:(id)sender
{
    //这个sender其实就是UIButton，因此通过sender.tag就可以拿到刚才的参数
    //int i = [sender tag];
}

//通知执行的函数
- (void)helpPlayerItemDidReachEnd:(NSNotification *)notification
{
    //获得当前通知传达的控件
    //    AVPlayer *mmmmavplayer=  notification.object;
    //获得播放的状态
    //    AVPlayerStatus  *stateus=   mmmmavplayer.status;
    
    //设置视频时间从0开始
    [self.avplayer seekToTime:kCMTimeZero];
    //播放视频
    [self.avplayer play];
    
}

//垃圾回收机制 并清除通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

//视图将要消失事件 停止播放
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.avplayer pause];
}

@end
